import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { AppEnvelope } from '../model/message';
import { RequestService } from './request.service';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class FileApiService {

  constructor(
    private request: RequestService
  ) { }

  public delete(fileUrl: string): Observable<AppEnvelope<any>> {
    const data = new FormData();
    data.append('objectName', this.convertFileUrlToObjectName(fileUrl));
    return this.request.post<AppEnvelope<any>>('/minio/delete', data)
  }

  private convertFileUrlToObjectName(fileUrl: string): string {
    return fileUrl.slice(fileUrl.indexOf(environment.bucketName) + environment.bucketName.length + 1);
  }
}
